﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace WebChatApi
{
    /// <summary>
    /// Class for Web dictionary, which is inherited from <see cref="Dictionary{T1,T2}"/> with key ignoring cases.
    /// </summary>
    public class WebDictionary : Dictionary<string, string>
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="WebDictionary"/> class.
        /// </summary>
        /// <param name="capacity">The initial number of elements that the <see cref="T:System.Collections.Generic.Dictionary`2" /> can contain.</param>
        public WebDictionary(int capacity)
            : base(capacity, StringComparer.OrdinalIgnoreCase)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebDictionary"/> class.
        /// </summary>
        public WebDictionary()
            : base(StringComparer.OrdinalIgnoreCase)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebDictionary"/> class.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        public WebDictionary(IDictionary<string, string> dictionary)
            : base(dictionary, StringComparer.OrdinalIgnoreCase)
        {
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Gets the value.
        /// If key is null or empty, or it is not contained in dictionary, return String.Empty.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            return (string.IsNullOrWhiteSpace(key) || !this.ContainsKey(key)) ? string.Empty : this[key];
        }

        /// <summary>
        /// Gets the HTML decoded value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public string GetHtmlDecodedValue(string key)
        {
            return WebUtility.HtmlDecode(GetValue(key));
        }

        #endregion
    }
}
